﻿using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using ProcurementManagement.Applcation.Command.ProcureApplyCommand;
using ProcurementManagement.Domain.Dtos;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Infrstructre.Imp;
using ProcurementManagement.Infrstructre.Interface;
using SqlSugar;
using System.Collections.Generic;
using static ERPPublic.ParseAPI;

namespace ProcurementManagement.Applcation.CommandHanlers.ProcureApplyCommandHandler
{
    /// <summary>
    /// 采购申请反填中介者
    /// </summary>
    public class ProcureApplyIdFanCommandHandler : IRequestHandler<ProcureApplyIdFanCommand, ProcureApplyFanDto>
    {
        protected readonly InterfaceVendorShowRepository vendorRepository;//供应商
        protected readonly lnterfaceProcureApplyRepository applyRepository; //采购申请仓库
        protected readonly lnterfacePurchaseproductRepository productRepository;//采购明细申请仓库
        protected readonly ISqlSugarClient db;

        public ProcureApplyIdFanCommandHandler(InterfaceVendorShowRepository vendorRepository, lnterfaceProcureApplyRepository applyRepository, lnterfacePurchaseproductRepository productRepository, ISqlSugarClient db)
        {
            this.vendorRepository = vendorRepository;
            this.applyRepository = applyRepository;
            this.productRepository = productRepository;
            this.db = db;
        }

        public async Task<ProcureApplyFanDto> Handle(ProcureApplyIdFanCommand request, CancellationToken cancellationToken)
        {

            var list = (await applyRepository.GetAll()).First(x => x.IsDel == false && x.ProcureApplyId == request.id);//获取采购申请列表
            //采购申请信息
            ProcureApplyFanDto dto = new ProcureApplyFanDto();
            dto.ProcureApplyId = list.ProcureApplyId;
            dto.PANumber = list.PANumber;
            dto.PADocumentsType = list.PADocumentsType;
            dto.PAApplicationType = list.PAApplicationType;
            dto.IsPAInquiry = list.IsPAInquiry;
            dto.PADocuments = list.PADocuments;
            dto.PAApplyMotif = list.PAApplyMotif;
            dto.PAEnquiryState = list.PAEnquiryState;
            dto.PAPurchaseState = list.PAPurchaseState;
            dto.Dates = list.Dates;
            dto.Efforts = list.Efforts;
            dto.PANumbers = list.PANumbers;
            dto.AddName = list.AddName;
            dto.AddTime = list.AddTime;
            dto.Office_Id = list.Office_Id;
            dto.User_Id = list.User_Id
                ;

            dto.list = await GetProductList("", "", 1, 100, list.PANumbers);
            return dto;
        }
        /// <summary>
        /// 显示产品信息
        /// </summary>
        /// <param name="PName"></param>
        /// <param name="PNumber"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<List<PurchaseproductDto>> GetProductList(string? PName, string? PNumber, int pindex, int psize, string id)
        {
            //产品
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIzMCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTgsMTkiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5Y2i6IqzIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.nm7UOrAYUuaVCB_tt7bptTzyJotLluZ6yYj0VipQh-w";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/erpcp/api/Product/ProductShow?PName=" + PName + "&PNumber=" + PNumber + "&ProductTypeId=" + 0 + "&PageIndex=" + pindex + "&PageSize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Products>>(postReponse);
            //明细表
            var newjson = (await productRepository.GetAll()).Where(x => x.PANumber == id).ToList();
            //供应商
            var list = (await vendorRepository.GetAll()).ToList();
            List<PurchaseproductDto> dto = new List<PurchaseproductDto>();
            foreach (var item in newjson)
            {
                var projson = json.data.list.First(x => x.ProductId == item.ProductId);
                var venjson = list.First(x => x.VendorId == item.VendorId);
                dto.Add(new PurchaseproductDto
                {
                    PurchaseproductId = item.PurchaseproductId,
                    pNumber = item.PANumber,
                    pSpec = projson.PSpec,
                    ProductId = projson.ProductId,
                    PName = projson.PName,
                    pUnit = projson.PUnit,
                    VendorId = venjson.VendorId,
                    VName = venjson.VName,
                    rPNum = item.Num,
                    rpPrice = item.Price,
                    SumPrice = item.SumPrice,
                    dates = item.Datess,
                    beizhu = item.Remark,
                });
            }
            return dto;
        }
        //产品
        public class Products
        {
            /// <summary>
            /// 产品Id
            /// </summary>
            [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
            public int ProductId { get; set; }
            /// <summary>
            /// 标识
            /// </summary>

            public int? PCharacteristic { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PNumber { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PName { get; set; }
            /// <summary>
            /// 规格型号
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PSpec { get; set; }
            /// <summary>
            /// 单位
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PUnit { get; set; }

            /// <summary>
            /// 产品分类ID
            /// </summary>

            public int ProductTypeId { get; set; }




        }

    }


}
